import axios from "axios";
import {ElMessage} from "element-plus";
import router from "../routes";

const service = axios.create({
    baseURL: '/',
    timeout: 6000
});

service.interceptors.response.use((response) => {

    if (response.data.code === 401) {

        ElMessage.error(response.data.msg);
        router.push('/login');
        return Promise.reject(response);

    } else if(response.data.code === 1){

        ElMessage.success(response.data.msg);
        return response.data;

    }else if(response.data.code === 0){

        ElMessage.error(response.data.msg);
        return Promise.reject(response);

    }

}, (error) => {

    ElMessage.error(error);
    return Promise.reject(error);

});

export default service
